package cn.pugle.datasketches.originalgo;

import lombok.Data;

import java.io.*;
import java.util.ArrayList;

/**
 * @author tzp
 * @since 2019/7/3
 */
public class GK {
    public static final String DS1 = "/Users/tzp/Documents/private/cnm/CodeAccumulate/src/main/resources/dataset100float";

    private static void sort() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(DS1));
        String line;
        ArrayList<Double> list = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            Double value = Double.valueOf(line);
            list.add(value);
        }
        list.sort(Double::compareTo);

        BufferedWriter bw = new BufferedWriter(
                new FileWriter(DS1 + "_sort"));
        for (Double value : list) {
            bw.write(String.format("%.2f", value));
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(DS1));
        BufferedWriter bw = new BufferedWriter(
                new FileWriter(DS1 + "_gk"));

        String line;
        ArrayList<Double> list = new ArrayList<>();
        Summary previousSummary = null;
        while ((line = br.readLine()) != null) {
            Double value = Double.valueOf(line);
            list.add(value);
            if (list.size() == 4) {
                list.sort(Double::compareTo);
                Summary newSummary = new Summary();
                newSummary.setMinValue(list.get(0));
                if (previousSummary == null) {
                    newSummary.setSummaryGap(1);
                } else {
                    newSummary.setSummaryGap(newSummary.minValue - previousSummary.minValue);
                }
                newSummary.setDelta(list.get(list.size() - 1) - list.get(0));

                bw.write(newSummary.toString());
                bw.newLine();
                previousSummary = newSummary;
                list.clear();
            }
        }

        bw.flush();
        bw.close();
    }

    @Data
    public static class Summary {
        double minValue;
        double summaryGap;
        double delta;

        @Override
        public String toString() {
            return "(" + minValue + "," + summaryGap + "," + delta + ")";
        }
    }
}
